var dialog;

window.onfocus = DialogFocus;


function GetHref()
{
    var url;
	var href;
	var pos;
	
	pos = window.location.href.indexOf('?');
	if (pos != -1)
	{
		href = window.location.href.substring(0,pos);
	}
	else
	{
		href = window.location.href;
	}
	
	pos = href.indexOf('#');
	if (pos != -1)
	{
		href = href.substring(0,pos);
	}
	else
	{
		href = href;
	}
	return href;
}

function DialogFocus()
{
	if(dialog != null)
	{
		try
		{
			dialog.focus();
		}
		catch(x)
		{}
	}
}

function Edit_Content(app)
{
	var url;
	var href;
	var canEdit = true;
	
	//test xml
    var XMLDoc = "<?xml version=\"1.0\"?><Root></Root>"; //XML Document;
    try 
    {
        var domDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
        domDoc.async=false;
        domDoc.loadXML(XMLDoc);
    }
    catch (e)
    {
        SiteNote.Debug.error('You need to install MSXML 6.0 to run SiteNOTE properly.\n Please check your security settings.\n Internet Explorer\'s security zone should be set to \"Trusted Sites\" or \"Local Intranet\".\n Please check the documentation for more information.');
        canEdit = false;
    }
    
    //test popup blocker
//    var pop = window.open('about:blank','','width=1,height=1,left=0,top=0,scrollbars=no');
//    if(!pop)
//    {
//        alert('You need to disable popup blocker to run SiteNOTE properly');
//        canEdit = false;
//    }
//    else
//    {
//        pop.close();
//    }
//	
//	if(canEdit)
//	{
	    href = GetHref();
    		
	    url = href + "?SiteNote_Mode=Edition";
	    window.navigate(url);
//	}
}

function Design(app)
{
	var url;
	var href;
	
	href = GetHref();
		
	url = href + "?SiteNote_Mode=Design";
	window.navigate(url);
}


function DesignEdit(app)
{
	var url;
	var href;
	
	
	href = GetHref();
	
	url = href + "?SiteNote_Mode=DesignEdit";
	window.navigate(url);
}


function Catalog(app)
{
	var url;
	var href;
	
	href = GetHref();
		
	url = href + "?SiteNote_Mode=Catalog";
	window.navigate(url);
}

function Create_Page(app)
{
	var url;
	var returnUrl;
	var href;
	
	href = GetHref();
	
	url = app + "/Admin/CreatePage.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	
	dialog = window.open(url, null, "height=300,width=350,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=no,status=no,scrollbars=no");
	dialog.opener = window;
	dialog.moveTo((window.screen.availWidth - 400) / 2, (window.screen.availHeight - 150) / 2);
	dialog.focus();
	
}

function Page_Properties(app)
{
	var url;
	var returnUrl;
	var href;
	var pos;
	
	href = GetHref();
	
	url = app + "/Admin/PageProperties.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	dialog = window.open(url, null, "height=400,width=440,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=no");
	dialog.opener = window;
	dialog.moveTo((window.screen.availWidth - 400) / 2, (window.screen.availHeight - 150) / 2);
	dialog.focus();
}

function Page_Security(app)
{
	var url;
	var returnUrl;
	var href;
	var pos;
	
	href = GetHref();
	
	url = app + "/Admin/PageSecurity.aspx";
	url += "?CurrentPage=" + encodeURIComponent(href);
	dialog = window.open(url, null, "height=300,width=300,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes");
	dialog.opener = window;
	dialog.focus();
}

function Website_Administration(app)
{
	var url;
	url = app + "/Admin/Websites.aspx";
	window.navigate(url);
}

//function Notifications(app) {
//    var url;
//    url = app + "/Admin/Notifications.aspx";
//    window.navigate(url);
//}

function Delete_Page(app, cmApp, defaultPage,parentPath)
{
	var result;
	var xmlHttp;
	var response;
	var response;
	var responseDocument;
	var status;
	var post;
	var href;
	var pos;
	
	href = GetHref();
    if  (SiteNote.MessageBox.confirm("<b style=\"color:red\">Delete this page? (The page will be deleted immediately in BOTH the offline AND the online sites; the site hierarchy will be automatically published)</b>", "question"))
	{
		try
		{
            var wsClient = new WSClient(app + cmApp + "/Admin/Webservices/SecurePageSvc.asmx", "POST", "http://tempuri.org/");
            wsClient.setSoapHeader("SessionIdHeader", {Id: SiteNote.Session.SessionId});
			response = wsClient.invoke("DeletePage" , {pageUrl: href});
			if (wsClient.getErrorCode() != 0)
			{
                SiteNote.Debug.error('Delete_Page - unable to execute request');
                SiteNote.Debug.error(wsClient.getErrorReason());
			}
			else
			{
                if (!response)
                {
                    throw "unable to retrieve read response";
                }
				/* MsXml modif */
				responseDocument = new ActiveXObject("Msxml2.DOMDocument");
				
				responseDocument.loadXML(response);
				
				if (responseDocument.parseError.errorCode != 0 || responseDocument.xml == '')
				{
					SiteNote.Debug.error("[ToolBar] Unable to retrieve read response");
				}
				else
				{
					status = responseDocument.documentElement.firstChild;

					if (status.text == 'succeed')
					{
						//alert('Delete page succeed');
						if(parentPath != "")
						{
						    window.navigate(parentPath);
						}
						else
						{
						    window.navigate(app + defaultPage + ".off.aspx");
						}
					}
					else
					{
                        SiteNote.Debug.error("[ToolBar] Delete page failed");
					}
				}
			}	
		}
		catch(e)
		{
			SiteNote.Debug.error("[ToolBar] Delete page error");
            SiteNote.Debug.error("[ToolBar] " + e.message);
		}	
	}
	
}

function LogOut(app,absPath)
{
	//if(window.confirm("Do you want to quit SiteNote Editor?(if you have not published the pages, your modifications will not appear online)"))
	//{
		var url;
		url = app + "/Admin/Logout.aspx?absPath=" + absPath;
		window.navigate(url);
	//}
}

function View_Basket(app)
{
	var url;
	var returnUrl;
	
	url = app + "/Admin/Basket.aspx";
	dialog = window.open(url, null, "height=500,width=400,channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,status=no,scrollbars=yes");
	dialog.opener = window;
	dialog.focus();
}

function Back_Page(app)
{
	var tmp;
	history.back();
}

function Move_Page(position, app)
{
	var result;
	var xmlHttp;
	var doc;
	var response;
	var responseDocument;
	var status;
	var post;
	var href;
	var pos;
	
	href = GetHref();
		
	try
	{
        var wsClient = new WSClient(app + "/Admin/Webservices/SecurePageSvc.asmx", "POST", "http://tempuri.org/");
        wsClient.setSoapHeader("SessionIdHeader", {Id: SiteNote.Session.SessionId});
	    response = wsClient.invoke("MovePage" , {pageUrl: href, position: position});

		if (wsClient.getErrorCode() != 0)
		{
            SiteNote.Debug.error("[ToolBar] Move_Page - unable to execute request");
            SiteNote.Debug.error("[ToolBar] " + wsClient.getErrorReason());
		}
		else
		{
            if (!response)
            {
                throw "unable to retrieve read response";
            }
			
			/* MsXml modif */
			responseDocument = new ActiveXObject("Msxml2.DOMDocument");
			
			responseDocument.loadXML(response);
			if (responseDocument.parseError.errorCode != 0 || responseDocument.xml == '')
			{
				SiteNote.Debug.error("[ToolBar][Move_Page] Unable to load response");
			}
			else
			{
				status = responseDocument.documentElement.firstChild;
				
				if (status.text == 'succeed')
				{
					SiteNote.Debug.info('Move page succeed');
					location.href = location.href
				}
				else
				{
					//alert('Move page failed');
				}
			}
		}	
	}
	catch(e)
	{
		SiteNote.Debug.error("[ToolBar] Move page error");
        SiteNote.Debug.error("[ToolBar] " + e.message);
	}	
}

function Save_Page()
{
	__doPostBack('', null);
}

function ViewHide(chkView,app,cmApp)
{
	var pos;
	var post;
	var href;
	var url;
	try
	{
		href = GetHref();
		
		post = "?pageUrl=" + encodeURIComponent(href);
					
		switch(chkView.name)
		{
			case "viewHidden":
				post += "&pagetype=hidden";
				break;
			case "viewOffline":
				post += "&pagetype=offline";
				break;
			default:break;
		}
		post += "&ischecked="+chkView.checked;
		url = app + cmApp + "/Admin/ChangeParam.aspx"+post;
		window.navigate(url);
	}
	catch(e)
	{
		SiteNote.Debug.error("[ToolBar][ViewHide] " + e.message); 
	}
}

function ViewHiddenCatalogZone()
{
    if(document.all["CatalogZoneDiv"] != null)
    {
        if (document.all["CatalogZoneDiv"].style.display == '')
        {
            document.all["CatalogZoneDiv"].style.display = 'none';
        }
        else
        {
            document.all["CatalogZoneDiv"].style.display = '';
        }
   }
}

if (location.href.indexOf('AutoSave=True') != -1)
{
    setTimeout('AutoSave()',5000);
}

function AutoSave()
{
    var buttonSave;
    buttonSave = document.all['ctl00_SAVE_PAGE'];
    if (buttonSave != null)
    {
       buttonSave.click();
    }
}




            
        
    
    

